package com.spring.theory.bean.beanFactoryPostProcessor.postProcessor;

import lombok.SneakyThrows;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;

/**
 * MapperPostProcessor
 *
 * @author H
 * @version 1.0
 * @date 2023-12-26 16:34
 */
public class MapperPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @SneakyThrows
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:com/spring/theory/service/bean/beanFactoryPostProcessor/model/**/*.class");
        CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();
        AnnotationBeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
        for (Resource resource : resources) {
            MetadataReader metadataReader = cachingMetadataReaderFactory.getMetadataReader(resource);
            ClassMetadata classMetadata = metadataReader.getClassMetadata();
            if (classMetadata.isInterface()) {
                /**
                 * 把每一个Mapper接口转换成MapperFactoryBean 放入bean容器
                 */
                BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(MapperFactoryBean.class)
                        //设置构造器
                        .addConstructorArgValue(classMetadata.getClassName())
                        //通过类型查找bean容器中的SqlSessionFactory，设置SqlSessionFactory工厂
                        .setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE)
                        .getBeanDefinition();

                //通过接口类名生成beanName; genericBeanDefinition 里面的类就是后面bean的名字
                BeanDefinition interfaceBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(classMetadata.getClassName()).getBeanDefinition();
                //根据bean定义里面的信息生成beanName，这里要传入接口bean定义
                String beanName = beanNameGenerator.generateBeanName(interfaceBeanDefinition, registry);
                registry.registerBeanDefinition(beanName, beanDefinition);
            }
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
